home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / toogl / regexp.h < prev    next >
C/C++ Source or Header  |  1996-11-11  |  4KB  |  147 lines

  1. /*
  2.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or publicity relating 
  9.  * to the software without the specific, prior written permission of 
  10.  * Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, 
  14.  * ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  15.  *
  16.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF 
  19.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT 
  20.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * version 1.6
  25.  * Regexp is a class that encapsulates the Regular expression
  26.  * stuff. Hopefully this means I can plug in different regexp
  27.  * libraries without the rest of my code needing to be changed.
  28.  * Written by Jim Morris,  jegm@sgi.com
  29.  */
  30. #ifndef    _REGEXP_H
  31. #define _REGEXP_H
  32. #include    <iostream.h>
  33. #include    <stdlib.h>
  34. #include    <malloc.h>
  35. #include    <string.h>
  36. #include    <assert.h>
  37. #include    <ctype.h>
  38.  
  39. #include    "regex.h"
  40.  
  41. /*
  42.  * Note this is an inclusive range where it goes
  43.  * from start() to, and including, end()
  44.  */
  45. class Range
  46. {
  47. private:
  48.     int st, en;
  49.     
  50. public:
  51.     Range()
  52.     {
  53.     st=0; en= -1;
  54.     }
  55.     
  56.     Range(int s, int e)
  57.     {
  58.     st= s; en= e;
  59.     }
  60.     
  61.     int start(void) const { return st;}
  62.     int end(void) const { return en;}
  63.     int length(void) const { return (en-st)+1;}
  64. };
  65.  
  66. class Regexp
  67. {
  68. public:
  69.     enum options {def=0, nocase=1};
  70.     
  71. private:
  72.     regexp *repat;
  73.     const char *target; // only used as a base address to get an offset
  74.     int res;
  75.     int iflg;
  76. #ifndef    __TURBOC__
  77.     void strlwr(char *s)
  78.     {
  79.     while(*s){
  80.         *s= tolower(*s);
  81.         s++;
  82.     }
  83.     }
  84. #endif    
  85. public:
  86.     Regexp(const char *rege, int ifl= 0)
  87.     {
  88.         iflg= ifl;
  89.         if(iflg == nocase){ // lowercase fold
  90.             char *r= new char[strlen(rege)+1];
  91.             strcpy(r, rege);
  92.             strlwr(r);
  93.             if((repat=regcomp(r)) == NULL){
  94.             cerr << "regcomp() error" << endl;
  95.             exit(1);
  96.             }
  97.             delete [] r;
  98.     }else{
  99.         if((repat=regcomp (rege)) == NULL){
  100.             cerr << "regcomp() error" << endl;
  101.             exit(1);
  102.         }
  103.         }
  104.     }
  105.     
  106.     ~Regexp()
  107.     {
  108.     free(repat);
  109.     }    
  110.  
  111.     int match(const char *targ)
  112.     {
  113.         int res;
  114.         if(iflg == nocase){ // fold lowercase
  115.             char *r= new char[strlen(targ)+1];
  116.             strcpy(r, targ);
  117.             strlwr(r);
  118.             res= regexec(repat, r); 
  119.             target= r; // looks bad but is really ok, really
  120.             delete [] r;
  121.         }else{
  122.         res= regexec(repat, targ);
  123.         target= targ;
  124.     }
  125.  
  126.     return ((res == 0) ? 0 : 1);
  127.     }
  128.     
  129.     int groups(void) const
  130.     {
  131.     int res= 0;
  132.     for (int i=0; i<NSUBEXP; i++) {
  133.         if(repat->startp[i] == NULL) break;
  134.         res++;
  135.     }
  136.     return res;
  137.     }
  138.     
  139.     Range getgroup(int n) const
  140.     {
  141.     assert(n < NSUBEXP);
  142.     return Range((int)(repat->startp[n] - (char *)target),
  143.              (int)(repat->endp[n] - (char *)target) - 1);
  144.     }
  145. };
  146. #endif
  147.